// api/cost.js
import request from '@/utils/request'

// ==================== 工时成本 API ====================

// 查询工时成本列表
export function listLaborCost(query) {
  return request({
    url: '/laborCost/list',
    method: 'get',
    params: query
  })
}

// 查询工时成本详细
export function getLaborCost(id) {
  return request({
    url: '/laborCost/' + id,
    method: 'get'
  })
}

// 新增工时成本
export function addLaborCost(data) {
  return request({
    url: '/laborCost',
    method: 'post',
    data: data
  })
}

// 修改工时成本
export function updateLaborCost(id, data) {
  return request({
    url: '/laborCost/' + id,
    method: 'put',
    data: data
  })
}

// 删除工时成本
export function delLaborCost(ids) {
  return request({
    url: '/laborCost/' + ids.join(','),
    method: 'delete'
  })
}

// 工时成本统计
export function getLaborStatistics() {
  return request({
    url: '/laborCost/statistics',
    method: 'get'
  })
}

// ==================== 报销成本 API ====================

// 查询报销成本列表
export function listReimbursementCost(query) {
  return request({
    url: '/reimbursementCost/list',
    method: 'get',
    params: query
  })
}

// 查询报销成本详细
export function getReimbursementCost(id) {
  return request({
    url: '/reimbursementCost/' + id,
    method: 'get'
  })
}

// 新增报销成本
export function addReimbursementCost(data) {
  return request({
    url: '/reimbursementCost',
    method: 'post',
    data: data
  })
}

// 修改报销成本
export function updateReimbursementCost(id, data) {
  return request({
    url: '/reimbursementCost/' + id,
    method: 'put',
    data: data
  })
}

// 删除报销成本
export function delReimbursementCost(ids) {
  return request({
    url: '/reimbursementCost/' + ids.join(','),
    method: 'delete'
  })
}

// 报销成本统计
export function getReimbursementStatistics() {
  return request({
    url: '/reimbursementCost/statistics',
    method: 'get'
  })
}

// ==================== 付款成本 API ====================

// 查询付款成本列表
export function listPaymentCost(query) {
  return request({
    url: '/paymentCost/list',
    method: 'get',
    params: query
  })
}

// 查询付款成本详细
export function getPaymentCost(id) {
  return request({
    url: '/paymentCost/' + id,
    method: 'get'
  })
}

// 新增付款成本
export function addPaymentCost(data) {
  return request({
    url: '/paymentCost',
    method: 'post',
    data: data
  })
}

// 修改付款成本
export function updatePaymentCost(id, data) {
  return request({
    url: '/paymentCost/' + id,
    method: 'put',
    data: data
  })
}

// 删除付款成本
export function delPaymentCost(ids) {
  return request({
    url: '/paymentCost/' + ids.join(','),
    method: 'delete'
  })
}

// 付款成本统计
export function getPaymentStatistics() {
  return request({
    url: '/paymentCost/statistics',
    method: 'get'
  })
}

// ==================== 综合统计 API ====================

// 获取总成本统计（前端需要自己计算三个统计的和）
export function getTotalCostSummary() {
  return Promise.all([
    getLaborStatistics(),
    getReimbursementStatistics(),
    getPaymentStatistics()
  ]).then(([laborRes, reimbursementRes, paymentRes]) => {
    // 这里根据后端返回的数据结构进行调整
    const laborTotal = laborRes.data?.total || 0
    const reimbursementTotal = reimbursementRes.data?.total || 0
    const paymentTotal = paymentRes.data?.total || 0

    return {
      totalLaborCost: laborTotal,
      totalReimbursementCost: reimbursementTotal,
      totalPaymentCost: paymentTotal,
      totalCost: laborTotal + reimbursementTotal + paymentTotal
    }
  })
}

// 获取项目列表（用于下拉框）
export function getProjectList() {
  return request({
    url: '/project/selectList',
    method: 'get'
  })
}

// 获取项目收入数据（合同金额）
export function getProjectRevenues() {
  return request({
    url: '/project/revenues/data',
    method: 'get'
  })
}
